package com.jjh.winterframework.util;

import com.jjh.winterframework.annatation.Component;

import java.io.*;
import java.lang.annotation.Annotation;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * winter框架提供的工具类
 * @author JJH
 */
public class WinterUtils {
      private static final String WINTER_BANNER = "src/main/resources/banner.txt";

    /**
     * 返回当前时间
     * @return
     */
    public static String getNowTime(){

       return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

    }




    /**
     * 打印winterLOGO
     */
    public static void printWinterLogo() {

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(WINTER_BANNER)));
            String msg = null;
            while ((msg = reader.readLine()) != null) {
                System.out.println(msg);
            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *  从文件路径截取出类名
     */
    public static String getClassName(String path){
        String replace = path.replace("\\", ".");
        int index = replace.lastIndexOf("classes");
        String substring = replace.substring(index+8);
        String classname = substring.substring(0, substring.lastIndexOf("class")-1);
        return classname;
    }


    /**
     *  递归遍历所有文件
     */
    public static void listFile(File file, List<File> fileList){
        if (!file.isDirectory()) {
            fileList.add(file);
        }else {
            File[] files = file.listFiles();
            for (File subFile : files) {
                listFile(subFile,fileList);
            }
        }
    }

    /**
     * 根据包名数组获取.class文件
     *
     * @param packagePaths 包名路径数组
     */
    public static List<File> getClassFileByPackage(String[] packagePaths){
        List<File> fileList = new ArrayList<>();
        ArrayList<File> classFileList = new ArrayList<>();
        for (String packge : packagePaths) {
            //把对应包名下的文件中含有@Component注解的类解析了，然后创建对象
            String replace = packge.replace(".", "/");
            URL url = ClassLoader.getSystemResource(replace);
            String path = url.getPath();
            //找该目录下的所有.class文件
            File file = new File(path);
            listFile(file,fileList);
            for (File fiLe : fileList) {
                String name = fiLe.getName();
                if(name.endsWith(".class")){
                    classFileList.add(fiLe);
                }
            }
        }
        return classFileList;
    }


    /**
     * 返回类路径下的所有.class文件
     */
    public static List<File> getClassFiles(){
        URL url = Thread.currentThread().getContextClassLoader().getResource("");

        String path = url.getPath();
        File file = new File(path);
        List<File> fileList = new ArrayList<>();
        listFile(file,fileList);
        ArrayList<File> classFileList = new ArrayList<>();
        for (File fiLe : fileList) {
            String name = fiLe.getName();
            if(name.endsWith(".class")){
                classFileList.add(fiLe);
            }
        }
        return classFileList;
    }

    /**
     * 获取类上有@Component该注解的类文件集合
     * @return 类文件集合
     */
    public static List<File> getClassFileByAnnatation(Class<Component> componentClass) {
        List<File> list = new ArrayList<>();
        List<File> classFiles = getClassFiles();
        for (File classFile : classFiles) {
            String path = classFile.getPath();
            String className = getClassName(path);
            Class<?> aClass = null;
            try {
                aClass = Class.forName(className);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            Annotation annotation = aClass.getAnnotation(componentClass);
            if(annotation!=null){
                //表示该类上有该注解，可以把该文件装入集合中
                list.add(classFile);
            }
        }
       return list;
    }
}
